//
//  RCRetweetedView.swift
//  RC-微博0305
//
//  Created by 任淳 on 16/3/12.
//  Copyright © 2016年 ReverChun. All rights reserved.
//

import UIKit
import SnapKit

class RCRetweetedView: UIView {

    //转发微博 也分有图片和没图片 要判断 不然间距会出错
    var bottomConstraints:Constraint?
    
    var status:RCUserStatus? {
        didSet{
            textLab.text = status?.text
            
            //这里需要的话 也要进行判断修改转发视图的约束
            //约束不一定时，要先卸载先前设置好的约束 在根据判断设置新的约束
            bottomConstraints?.uninstall()
            if let urls = status?.imageUrl where urls.count != 0 {
                //有图片的时候
                picture.imageUrls = urls
                picture.hidden = false
                //每个部分视图的底部约束，和自己子视图的地步不要有偏移量 offset 不然会多出来一块
                self.snp_updateConstraints { (make) -> Void in
                    bottomConstraints = make.bottom.equalTo(picture).constraint
                }
            }else {
                //转发微博没有图片的时候
                //每个部分视图的底部约束，和自己子视图的地步不要有偏移量 offset 不然会多出来一块
                picture.imageUrls = nil
                picture.hidden = true
                self.snp_updateConstraints { (make) -> Void in
                    bottomConstraints = make.bottom.equalTo(textLab).constraint
                }
            }
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.96, alpha: 1)
        
        addSubview(textLab)
        addSubview(picture)
        
        setContraints()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setContraints() {
    
        textLab.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-10)
        
        }
        
        picture.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLab.snp_bottom).offset(10)
            make.left.equalTo(textLab)
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraints = make.bottom.equalTo(picture).offset(10).constraint
        }
        
        
    }
    
    
    //被转发微博的文字
    lazy var textLab:UILabel = UIFactory.labelWith("我哈哈哈哈哈哈哈哈", textColor: UIColor.darkGrayColor(), fontSize: 18, alignment: NSTextAlignment.Left)
    
    //被转发微博的图片
    lazy var picture:RCPictureView = RCPictureView()
    
    
}
